﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using BulkRenamer.Common.Models;
using BulkRenamer.Util.Enums;
#endregion

namespace BulkRenamer.App.Views
{
    public partial class frmPathHistorySelector : Form
    {
        #region Declarations
        const int widthLastAccessed = 120;
        #endregion
               
        #region Properties
        internal string SelectedPath { get; set; }
        #endregion

        #region Constructors
        public frmPathHistorySelector(ObservableCollection<PathHistory> pathSuggestions)
        {
            InitializeComponent();

            dgPathSuggestion.DataSource = pathSuggestions;
        }
        #endregion

        #region Destructors
        ~frmPathHistorySelector()
        {
            this.Dispose();
            GC.Collect();
        }
        #endregion

        #region Events
        private void dgPathSuggestion_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Form.ModifierKeys == Keys.Control)
            {
                SelectedPath = Convert.ToString(dgPathSuggestion.Rows[e.RowIndex].Cells["Path"].Value);
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Resize_Columns(object sender, EventArgs e)
        {
            SetColumnWidth("LastAccessed", eAllowance.None);
            SetColumnWidth("Path", eAllowance.None);
        }

        private void dgPathSuggestion_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            eAllowance allowance = eAllowance.None;
            string dcName = e.Column.Name;

            switch (dcName)
            {
                case "LastAccessed":
                    allowance = eAllowance.None;
                    break;
                case "Path":
                    allowance = eAllowance.More;
                    break;
                default:
                    break;
            }

            SetColumnWidth(dcName, allowance);
        }
        #endregion

        #region Methods
        private void SetColumnWidth(string colName, eAllowance allowance)
        {
            if (dgPathSuggestion.Columns.Contains(colName))
            {
                DataGridViewColumn dc = dgPathSuggestion.Columns[colName];
                int width = 0;

                switch (colName)
                {
                    case "LastAccessed":
                        width = widthLastAccessed;
                        break;
                    case "Path":
                        width = dgPathSuggestion.Width - widthLastAccessed - 6;
                        break;
                    default:
                        break;
                }

                if (allowance == eAllowance.None || (allowance == eAllowance.Less & dc.Width > width) || (allowance == eAllowance.More & dc.Width < width))
                    dc.Width = width;
            }
        }
        #endregion
    }
}
